import Vue from 'vue';
import Router from 'vue-router';
import store from '../store';
/* Layout */
import Layout from '@/layout';

Vue.use(Router);
/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true,
  },
  {
    path: '/register',
    component: () => import('@/views/register/index'),
    hidden: true,
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true,
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: '工作台',
        component: () => import('@/views/dashboard/index'),
        meta: { title: '工作台', icon: 'el-icon-s-platform' },
      },
    ],
  },

  {
    path: '/worker',
    component: Layout,
    name: '工人管理',
    meta: { title: '工人管理', icon: 'el-icon-s-open' },

    children: [
      // {
      //   path: '',
      //   name: '',
      //   component: () => import('@/views/worker/noSign'),
      //   meta: { title: '未发起合同', icon: 'el-icon-warning-outline', activeMenu: '/worker/init' },
      //   hidden: true,
      // },
      {
        path: 'init',
        name: '未发起合同',
        component: () => import('@/views/worker/noSign'),
        meta: { title: '未发起合同', icon: 'el-icon-warning-outline' },
      },
      {
        path: 'waitSign',
        name: '待签署',
        component: () => import('@/views/worker/waitSign'),
        meta: { title: '待签署', icon: 'el-icon-edit-outline' },
      },
      {
        path: 'waitSend',
        name: '待派遣',
        component: () => import('@/views/worker/waitSend'),
        meta: { title: '待派遣', icon: 'el-icon-s-promotion' },
      },
      {
        path: 'send',
        name: '已派遣',
        component: () => import('@/views/worker/send'),
        meta: { title: '已派遣', icon: 'el-icon-folder-checked' },
      },
      {
        path: 'leaveoff',
        name: '已离职',
        component: () => import('@/views/worker/leaveoff'),
        meta: { title: '已离职', icon: 'el-icon-folder-delete' },
      },
      {
        path: 'detail/:workerId',
        name: '工人详情',
        component: () => import('@/views/worker/workerDetail'),
        meta: { title: '工人详情', icon: 'el-icon-document-delete', activeMenu: '/worker' },
        hidden: true,
      },
    ],
  },
  {
    path: '/tenant',
    component: Layout,
    name: '用工单位',
    meta: { title: '用工单位', icon: 'el-icon-office-building' },
    children: [
      {
        path: 'undisturbed',
        name: '未发起',
        component: () => import('@/views/tenant/undisturbed'),
        meta: { title: '未发起', icon: 'el-icon-document-delete' },
      },
      {
        path: 'init',
        name: '待签署',
        component: () => import('@/views/tenant/init'),
        meta: { title: '待签署', icon: 'el-icon-document-remove' },
      },
      {
        path: 'all',
        name: '已签署',
        component: () => import('@/views/tenant/all'),
        meta: { title: '已签署', icon: 'el-icon-document-checked' },
      },
      {
        path: 'detail',
        name: '合同详情',
        component: () => import('@/views/tenant/detail'),
        meta: { title: '合同详情', icon: 'el-icon-s-custom', activeMenu: '/tenant' },
        hidden: true,
      },
    ],
  },
  {
    path: '/seal-manager',
    component: Layout,
    meta: { title: '印章管理', icon: 'el-icon-s-check' },
    children: [
      {
        path: '',
        name: '印章管理',
        component: () => import('@/views/seal/index'),
        meta: { title: '印章管理', icon: 'el-icon-s-check', breadcrumb: false },
      },
      {
        path: 'detail',
        name: '印章详情',
        meta: { title: '印章详情', icon: 'el-icon-s-check', activeMenu: '/seal-manager' },
        component: () => import('@/views/seal/detail'),
        hidden: true,
      },
      {
        path: 'add',
        name: '添加印章',
        meta: { title: '添加印章', icon: 'el-icon-s-check', activeMenu: '/seal-manager' },
        component: () => import('@/views/seal/add'),
        hidden: true,
      },
    ],
  },

  {
    path: '/template',
    component: Layout,
    meta: { title: '模板管理', icon: 'el-icon-document' },
    children: [
      {
        path: '',
        name: '模板管理',
        component: () => import('@/views/template/index'),
        meta: { title: '模板管理', activeMenu: '/template', breadcrumb: false },
      },
      {
        path: 'detail',
        name: '模板详情',
        component: () => import('@/views/template/detail'),
        meta: { title: '模板详情', icon: 'el-icon-s-custom', activeMenu: '/template' },
        hidden: true,
      },
    ],
  },
  {
    path: '/contract',
    component: Layout,
    redirect: '/contract/labor',
    name: '合同管理',
    meta: { title: '合同管理', icon: 'el-icon-document-copy' },
    children: [
      {
        path: 'labor',
        name: 'labor',
        component: () => import('@/views/contract/labor'),
        meta: { title: '劳动合同', icon: 'el-icon-notebook-1' },
      },
      {
        path: 'laborDispatch',
        name: '劳务派遣合同',
        component: () => import('@/views/contract/laborDispatch'),
        meta: { title: '劳务派遣合同', icon: 'el-icon-notebook-2' },
      },
      {
        path: 'detail/:flowId',
        name: '合同详情',
        component: () => import('@/views/contract/detail'),
        meta: { title: '合同详情', icon: 'form' },
        hidden: true,
      },
    ],
  },
  {
    path: '/safedeal',
    component: Layout,
    name: 'safedeal',
    meta: { title: '安全承诺书', icon: 'el-icon-bell' },
    children: [
      {
        path: 'unsigned',
        name: 'safedeal/unsigned',
        component: () => import('@/views/safedeal/unsigned'),
        meta: { title: '待签署', icon: 'el-icon-edit-outline' },
      }, {
        path: 'signed',
        name: 'safedeal/signed',
        component: () => import('@/views/safedeal/signed'),
        meta: { title: '已签署', icon: 'el-icon-document-checked' },
      }],
  },
  {
    path: '/system',
    component: Layout,
    name: '系统管理',
    meta: { title: '系统管理', icon: 'el-icon-setting' },

    children: [
      {
        path: 'user',
        name: '用户管理',
        component: () => import('@/views/system/user'),
        meta: { title: '用户管理', icon: 'el-icon-s-custom' },
      },
      {
        path: 'roles',
        name: '角色管理',
        component: () => import('@/views/system/role'),
        meta: { title: '角色管理', icon: 'el-icon-notebook-2' },
      },
      {
        path: 'external-service',
        name: '扩展服务',
        component: () => import('@/views/system/external-service'),
        meta: { title: '扩展服务', icon: 'el-icon-suitcase' },
        hidden: () => !store.state.user.isAdmin,
      },
      {
        path: 'setting',
        name: 'setting',
        component: () => import('@/views/system/setting'),
        meta: { title: '配置管理', icon: 'el-icon-setting' },
      },
    ],
  },
  { path: '*', redirect: '/404', hidden: true },
];

const createRouter = () =>
  new Router({
    // mode: 'history', // require service support
    base: '/',
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes,
    mode: 'history',
  });

const router = createRouter();

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter();
  router.matcher = newRouter.matcher; // reset router
}

export default router;
